#!/bin/sh

revision="setlang v0.2.3 (2023-06-09 MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
fi
echo -ne "$ME-$thisname---->\n" | $TEE -i -a $LOG

if [ -f $TMP/backup.mib ] || [ -f $TMP/reboot.mib ] || [ -f $TMP/flash.mib ]; then
	echo -ne "Aborted because backup/reboot or flash process is running...\n" | $TEE -a $LOG
	return 2> /dev/null
fi

if [ "$2" = "-noboot" ]; then
	noboot=$2
else
	noboot=""
fi

#Get the real train not from GLOBALS but from EEPROM as it can be changed already
TRAINVERSION="$($E2P r 3A0 19 | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p' | $SED -rn 's:\W*(\S\S)\W*:0x\1\n:pg' | $SED -rn '/^0x/p' | $XXD -r -p | $SED 's/[^a-zA-Z0-9_-]//g' )"

DATASET_2D00=""
DATASET_280=""

case $1 in
	-eu) {
		trap '' 2
		case $TRAINVERSION in
			MHIG_EU_*)
				echo -ne "Writing EU dataset 0x2C0 (visible_MMI_language_dataset):\n" | $TEE -a $LOG
				echo -ne "en_GB,cs_CZ,de_DE,nl_NL,fr_FR,it_IT,pl_PL,pt_PT,ru_RU,es_ES,sv_SE,tr_TR\n" | $TEE -a $LOG
				DATASET_2D00=0C656E5F474263735F435A64655F44456E6C5F4E4C66725F465269745F4954706C5F504C70745F505472755F525565735F455373765F534574725F5452000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031301095
				;;
			MHI2_ER_AUG22*)
				echo -ne "Writing EU dataset 0x2D00 (visible_MMI_language_dataset):\n" | $TEE -a $LOG
				echo -ne "de_DE,en_GB,en_SA,fr_FR,it_IT,es_ES,pt_PT,pt_BR,nl_NL,ru_RU,pl_PL,\n" | $TEE -a $LOG
				echo -ne "cs_CZ,es_MX,en_US,sv_SE,tr_TR,hu_HU,no_NO,ar_SA\n" | $TEE -a $LOG
				DATASET_2D00=1364655F444503656E5F474203656E5F53410266725F46520169745F49540165735F45530170745F50540170745F4252016E6C5F4E4C0172755F525501706C5F504C0163735F435A0165735F4D5801656E5F55530173765F53450174725F54520268755F4855016E6F5F4E4F0161725F5341010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003033AB9A
				;;
			*ER_AU276*|*ER_AU43x*|*ER_AU62x*|MHI2Q_ER_AUG22*)
				echo -ne "Writing EU dataset 0x2D00 (visible_MMI_language_dataset):\n" | $TEE -a $LOG
				echo -ne "de_DE,en_GB,en_SA,fr_FR,it_IT,es_ES,pt_PT,pt_BR,nl_NL,ru_RU,pl_PL,\n" | $TEE -a $LOG
				echo -ne "cs_CZ,es_MX,en_US,sv_SE,tr_TR,hu_HU,no_NO,ar_SA,da_DK,ms_MY,\n" | $TEE -a $LOG
				echo -ne "ro_RO,sl_SI,fi_FI,el_GR,uk_UA\n" | $TEE -a $LOG
				DATASET_2D00=1A64655F444503656E5F474203656E5F53410266725F46520169745F49540165735F45530170745F50540170745F4252016E6C5F4E4C0172755F525501706C5F504C0163735F435A0165735F4D5801656E5F55530173765F53450174725F54520268755F4855016E6F5F4E4F0161725F53410164615F444B016D735F4D5900726F5F524F01736C5F53490166695F464901656C5F475201756B5F5541010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003033DAE5
				;;
			*ER_AU37x*)
				echo -ne "Writing EU dataset 0x2D00 (visible_MMI_language_dataset):\n" | $TEE -a $LOG
				echo -ne "en_GB_cs_CZ_de_DE_nl_NL_fr_FR_it_IT_pl_PL_pt_PT_ru_RU_es_ES_sv_SE,\n" | $TEE -a $LOG
				echo -ne "tr_TR_no_NO_hu_HU_da_DK_fi_FI_sl_SI_uk_UA_ro_RO_el_GR\n" | $TEE -a $LOG
				DATASET_2D00=14656E5F47420163735F435A0164655F4445016E6C5F4E4C0166725F46520169745F495401706C5F504C0170745F50540172755F52550165735F45530173765F53450174725F5452026E6F5F4E4F0168755F48550164615F444B0166695F464901736C5F534901756B5F554101726F5F524F01656C5F47520100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031342E43
				;;
			*ER_AU57x*)
				echo -ne "Writing EU dataset 0x2D00 (visible_MMI_language_dataset):\n" | $TEE -a $LOG
				echo -ne "de_DE,en_GB,en_SA,fr_FR,it_IT,es_ES,pt_PT,pt_BR,nl_NL,ru_RU,pl_PL,\n" | $TEE -a $LOG
				echo -ne "cs_CZ,es_MX,en_US,sv_SE,tr_TR,ar_SA,en_SA\n" | $TEE -a $LOG
				DATASET_2D00=1264655F444503656E5F474203656E5F53410266725F46520169745F49540165735F45530170745F50540170745F4252016E6C5F4E4C0172755F525501706C5F504C0163735F435A0165735F4D5801656E5F55530173765F53450174725F54520261725F534101656E5F5341020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003033BA10
				;;
			*ER_BYSUV*|*ER_POG*)
				echo -ne "Writing EU dataset 0x2D00 (visible_MMI_language_dataset):\n" | $TEE -a $LOG
				echo -ne "de_DE,en_GB,en_SA,fr_FR,it_IT,es_ES,pt_PT,pt_BR,nl_NL,ru_RU,pl_PL,\n" | $TEE -a $LOG
				echo -ne "cs_CZ,es_MX,en_US,sv_SE,tr_TR,hu_HU,no_NO,ar_SA,el_GR,fi_FI,hr_HR,\n" | $TEE -a $LOG
				echo -ne "da_DK,sl_SI,sk_SK,sr_RS,ro_RO,bs_BA\n" | $TEE -a $LOG
				DATASET_2D00=1C64655F444503656E5F474203656E5F53410266725F46520169745F49540165735F45530170745F50540170745F4252016E6C5F4E4C0172755F525501706C5F504C0163735F435A0165735F4D5801656E5F55530173765F53450174725F54520268755F4855016E6F5F4E4F0161725F534101656C5F47520166695F46490168725F48520164615F444B01736C5F534901736B5F534B0173725F525301726F5F524F0162735F42410100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030333144
				;;
			*ER_SEG*|*ER_SKG*|*ER_VWG*)
				echo -ne "Writing EU dataset 0x2D00 (visible_MMI_language_dataset):\n" | $TEE -a $LOG
				echo -ne "de_DE,en_GB,fr_FR,it_IT,es_ES,nl_NL,cs_CZ,pt_PT,sv_SE,tr_TR,ru_RU,\n" | $TEE -a $LOG
				echo -ne "pl_PL,no_NO,en_US,es_MX,pt_BR,ar_SA,bs_BA,ro_RO,sk_SK,sl_SI,sr_RS,\n" | $TEE -a $LOG
				echo -ne "da_DK,el_GR, hr_HR,hu_HU,fi_FI,bg_BG,lv_LV,et_EE,lt_LT,uk_UA\n" | $TEE -a $LOG
				DATASET_2D00=2064655F444503656E5F47420366725F46520169745F49540165735F4553016E6C5F4E4C0163735F435A0170745F50540173765F53450174725F54520272755F525501706C5F504C016E6F5F4E4F01656E5F55530165735F4D580170745F42520161725F53410162735F424101726F5F524F01736B5F534B01736C5F53490173725F52530164615F444B01656C5F47520168725F48520168755F48550166695F46490162675F4247016C765F4C560165745F4545016C745F4C5401756B5F554101656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F47420163735F435A01656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F474201656E5F47420172755F52550130336D01
				;;
			*)
				echo -ne "Aborted. No supported train found!\n" | $TEE -a $LOG
				exit 1 ;;
		esac
		echo -ne "Writing en_GB dataset 0x280 (language dataset)\n" | $TEE -a $LOG
		DATASET_280=656E5F474200656E5F474200656E5F4742004331E45D
	};;

	-ar) {
		trap '' 2
		if [[ "$TRAINVERSION" = *ER* ]]; then
			echo -ne "visible_MMI_language_dataset - 0x2d00 - Arabic only\n" | $TEE -a $LOG
			echo -ne "en_GB,ar_SA"
			DATASET_2D00=02656E5F47420261725F5341020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003332C898
			echo -ne "language dataset - 0x280 - ar_SA\n" | $TEE -a $LOG
			DATASET_280=61725F53410161725F53410161725F5341014431A8FF
		else
			echo -ne "Aborted. No supported train found!\n" | $TEE -a $LOG
			exit 1
		fi
	};;

	-nar) {
		trap '' 2
		if [[ "$TRAINVERSION" = *US* ]]; then
			echo -ne "visible_MMI_language_dataset - 0x2d00 - NAR\n" | $TEE -a $LOG
			echo -ne "en_US,es_MX,fr_CA"
			DATASET_2D00=03656E5F55530166725F43410165735F4D58010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003431E3E4
			echo -ne "language dataset - 0x280 - en_US\n" | $TEE -a $LOG
			DATASET_280=656e5f555300656e5f555300656e5f5553004530c9a6
		else
			echo -ne "Aborted. No supported train found!\n" | $TEE -a $LOG
			exit 1
		fi
	};;

	-jp) {
		trap '' 2
		if [[ "$TRAINVERSION" = *JP* ]]; then
			echo -ne "visible_MMI_language_dataset - 0x2d00 - Japan\n" | $TEE -a $LOG
			echo -ne "ja_JP,en_US"
			DATASET_2D00=02656e5f5553016a615f4a5001656e5f555301656e5f555301000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000363090a9
			echo -ne "language dataset - 0x280 - ja_JP\n" | $TEE -a $LOG
			DATASET_280=6a615f4a50006a615f4a50006a615f4a500033335be3
		else
			echo -ne "Aborted. No supported train found!\n" | $TEE -a $LOG
			exit 1
		fi
	};;

	-kr) {
		trap '' 2
		if [[ "$TRAINVERSION" = *KR* ]]; then
			echo -ne "visible_MMI_language_dataset - 0x2d00 - Korea\n" | $TEE -a $LOG
			echo -ne "ko_KR,en_KR"
			DATASET_2D00=026b6f5f4b5201656e5f4b52010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003731cc83
			echo -ne "language dataset - 0x280 - ko_KR\n" | $TEE -a $LOG
			DATASET_280=6b6f5f4b52006b6f5f4b52006b6f5f4b52004c30a910
		else
			echo -ne "Aborted. No supported train found!\n" | $TEE -a $LOG
			exit 1
		fi
	};;

	-tw) {
		trap '' 2
		if [[ "$TRAINVERSION" = *TW* ]]; then
			echo -ne "visible_MMI_language_dataset - 0x2d00 - Taiwan\n" | $TEE -a $LOG
			echo -ne "zh_TW,en_US"
			DATASET_2D00=027a685f545701656e5f5457010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003831ba2c
			echo -ne "language dataset - 0x280 - zh_TW\n" | $TEE -a $LOG
			DATASET_280=7a685f5457007a685f5457007a685f5457004a307125
		else
			echo -ne "Aborted. No supported train found!\n" | $TEE -a $LOG
			exit 1
		fi
	};;

	-cn) {
		trap '' 2
		if [[ "$TRAINVERSION" = *CN* ]]; then
			echo -ne "visible_MMI_language_dataset - 0x2d00 - China\n" | $TEE -a $LOG
			echo -ne "zh_CN,zh_HK,en_CN"
			DATASET_2D00=037a685f434e017a685f484b01656e5f434e0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035318ba2
			echo -ne "language dataset - 0x280 - zh_CN\n" | $TEE -a $LOG
			DATASET_280=7a685f434e007a685f434e007a685f434e004830a705
			echo -ne "Language changed. DONE!\n" | $TEE -a $LOG
		else
			echo -ne "Aborted. No supported train found!\n" | $TEE -a $LOG
			exit 1
		fi
	};;

# help or unknown parameter ------------------------------
	*) {
		noboot=1
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION] [-noboot]"
		echo ""
		echo "Options:"
		echo "        -eu       set EU languages"
		echo "        -ar       set Arabic language"
		echo "        -nar      set NAR language"
		echo "        -kr       set Korean language"
		echo "        -tw       set Taiwan language"
		echo "        -cn       set Chinese language"
		echo "        -jp       set Japan language"
		echo "        --help    show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
	};;
esac

if [ ! -z "$DATASET_280" ]; then
	echo -ne "Dataset 2D00 before the change:" >> $LOG
	$UDP -key 3221422086 -ns 0 -type b | sed -n "s/^.*'\(.*\)'.*$/\1/p" | sed 's/ //g' 2> /dev/null >> $LOG
	$PERSW -f -P 0 3221422086 $DATASET_2D00 >> $LOG
	echo -ne "Dataset 2D00 after the change: " >> $LOG
	$UDP -key 3221422086 -ns 0 -type b | sed -n "s/^.*'\(.*\)'.*$/\1/p" | sed 's/ //g' 2> /dev/null >> $LOG
	
	echo -ne "Dataset 280 before the change:" >> $LOG
	$UDP -key 3221422085 -ns 0 -type b | sed -n "s/^.*'\(.*\)'.*$/\1/p" | sed 's/ //g' 2> /dev/null >> $LOG
	$PERSW -f -P 0 3221422085 $DATASET_280 >> $LOG
	echo -ne "Dataset 280 after the change: " >> $LOG
	$UDP -key 3221422085 -ns 0 -type b | sed -n "s/^.*'\(.*\)'.*$/\1/p" | sed 's/ //g' 2> /dev/null >> $LOG
fi

echo -ne "\n$thisname done!\n" | $TEE -i -a $LOG

trap 2

[ -z "$noboot" ] && . $thisdir/reboot -t 10
return 2> /dev/null

exit 0
